﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Dreamz
{
    public class Rum13
    {
        static public void Apply()
        {
            GameEngine.S.World[1, 5].OnArrival += new EventHandler(StartGame);
        }

        static public void StartGame()
        {
            //Opportunity to leave or start the game
            Console.WriteLine("Do you want to sit down? Y or N");
            ConsoleKeyInfo YorN = Console.ReadKey();

            if (YorN.KeyChar.ToString().ToLower() == "y")
            {
                GameEngine.S.ScreenDrawer.Show("Ready...", 55, 25);
                Thread.Sleep(1200);
                GameEngine.S.ScreenDrawer.Show("Set..", 55, 25);
                Thread.Sleep(1200);
                GameEngine.S.ScreenDrawer.Show("GO!", 55, 25);
                Thread.Sleep(500);

                List<string> wordList = new List<string>();

                wordList.Add(" ");
                wordList.Add("THIS");
                wordList.Add("IS");
                wordList.Add("THE");
                wordList.Add("MATRIX");
                wordList.Add("AND");
                wordList.Add("YOU");
                wordList.Add("PICKED");
                wordList.Add("THE");
                wordList.Add("RED");
                wordList.Add("PILL");
                wordList.Add(" ");
                wordList.Add("YOU");
                wordList.Add("HAVE");
                wordList.Add("TO");
                wordList.Add("REMOVE");
                wordList.Add("THESE");
                wordList.Add("WORDS");
                wordList.Add("BEFORE");
                wordList.Add("THEY");
                wordList.Add("HIT");
                wordList.Add("THE");
                wordList.Add("OTHER");
                wordList.Add("SIDE");

                string spacer = "";
                int counter = 0;
                string input = "";
                int thecount = wordList.Count();

                //The game itself
                while (true)
                {
                    
                    spacer += " ";
                    counter++;
                    //Game over if the list of words touch the other side.
                    if (counter == Console.WindowWidth)
                    {
                       //This gives the sad news that your character have died, and resets the game.
                        Console.WriteLine("You failed the test, and feel the sharp pain as the spikes impale your body from all sides.");
                        GameEngine.S.WriteColored("[GAME OVER]\n", ConsoleColor.Red);
                        Console.ReadKey(); 
                        Console.ReadKey();
                        GameEngine.S.Menu();
                    }
                    //This checks if the input corresponds to any word on the WordList and if so, replaces it with empty spaces. 
                    //Clear after pressing enter.
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo info = Console.ReadKey(true);

                        if (info.Key == ConsoleKey.Enter)
                        {
                            for (int i = 0; i < wordList.Count; i++)
                            {
                                if (input.ToUpper() == wordList[i])
                                {
                                    wordList.Remove(input.ToUpper());
                                    wordList.Insert(i, " ");
                                    input = "";
                                    --thecount;
                                    break;
                                }
                            }
                            input = "";
                        }
                        //Enable backspace while typing in KeyChar, else
                        else if (info.Key == ConsoleKey.Backspace)
                        {
                            if (input != "")
                                input = input.Substring(0, input.Length - 1);
                        }


                        else
                            input += info.KeyChar;

                    }
                    //This makes all the words on the List, moving to the right
                    for (int i = 0; i < wordList.Count; i++)
                    {
                        Console.WriteLine(spacer + wordList[i]);

                    }

                    Console.WriteLine("\n\n\n\nHints: Type and Enter. \nInput: " + input);
                    Thread.Sleep(150);
                    Console.Clear();

                    if (thecount == 2)
                    {
                    //This happens if you succed to "clear" the words
                        GameEngine.S.ScreenDrawer.Show("Congratulation! You just won nothing!", 80, 25);
                        Thread.Sleep(3000);
                        GameEngine.S.Player.Go("east");
                        break;
                    }
                }
            }
                else
                {
                    GameEngine.S.Player.Go("east");
                }

            }
        }
    }

